package com.gw.booking.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SignFilter implements Filter {

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
        
		HttpServletRequest hRequest = (HttpServletRequest)request;
        
        ((HttpServletResponse)response).setHeader("Access-Control-Allow-Origin", ((HttpServletRequest)request).getHeader("Origin"));
        ((HttpServletResponse)response).setHeader("Access-Control-Allow-Headers", "sign,token,Content-Type,cache-control,X-Requested-With");  
        ((HttpServletResponse)response).setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");  
        ((HttpServletResponse)response).setHeader("Access-Control-Allow-Credentials", "true");
		((HttpServletResponse)response).setHeader("Content-Type", "application/json;charset=utf-8");
		
		if(hRequest.getRequestURI().endsWith("api-docs.do")
				|| hRequest.getRequestURI().endsWith("upload.do")) {
			chain.doFilter(request, response);
			return;
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		
	}
}
